#ifndef f_utils_h_
#define f_utils_h_

#include <algorithm>

namespace Utils
{

template<class T>
void SafeDelete(T** ptr)
{
	if(ptr && *ptr)
	{
		delete (*ptr);
		(*ptr) = 0;
	}
}

template<class T>
void SafeRelease(T* ptr)
{
	if(ptr)
	{
		ptr->Release();
	}
}


	//------------------------------------------------------
	
	struct delete_pair_ptr
	{
		template<class T> void operator ()(T& r)
		{
			delete r.second; r.second = 0;
		}
	};
	
	struct delete_value_ptr
	{
		template<class T> void operator ()(T* ptr)
		{
			delete ptr;
		}
	};
	
	struct release_pair_ptr
	{
		template<class T> void operator ()(T& r)
		{
			if(r.second)
			{
				(r.second)->Release();
			}
		}
	};

	struct release_value_ptr
	{
		template<class T> void operator ()(T* ptr)
		{
			if(ptr)
			{
				ptr->Release();
			}
		}
	};

	//---------------------------------------------------------

	template<class container>
	void EmptyPtrMap(container *tmap)
	{
		if(!tmap)
			return;

		std::for_each(tmap->begin(), tmap->end(), delete_pair_ptr() );

		tmap->clear();
	}


	template<class container>
	void ReleasePtrMap(container *tmap)
	{
		if(!tmap)
			return;

		std::for_each(tmap->begin(), tmap->end(), release_pair_ptr() );

		tmap->clear();
	
	}

	template<class container>
	void EmptyPtrList(container *tlist)
	{
		if(!tlist)
			return;

		std::for_each(tlist->begin(), tlist->end(), delete_value_ptr() );

		tlist->clear();

	}
	

};

#endif